home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / Network / PowerScript 14 / PowerDown < prev    next >
Encoding:
Text File  |  1999-03-04  |  9.1 KB  |  332 lines  |  [TEXT/ToyS]

  1. (*
  2. V1.13 ◊ This script is PD 1998 by AKUA interactive media AG.
  3.  
  4. Drop a remote application onto PowerDown to configure it. Drop the Finder from a remote machine onto PowerDown to have the choice of Restart, Shutdown or Quit or drop any application to have it quit when this script is subsequently run. Drop a folder or volume to have this script unmount it when run
  5. V1.13 - 981019 - Change operation to open items if they are dropped on us
  6. V1.12 - 981005 - V1.26 of Akua Sweets / MacOS 8.5
  7. V1.10 - Fix gasLinkNeed variable syntax
  8. V1.09 - Let user choose item if run unactivated. Support new KeyChain format.
  9. V1.08 - Remove all Server support. Only linking to slim it down.
  10. V1.07 - Add gossip functionality
  11. *)
  12.  
  13. -- User setable properties
  14. property kasAllowAlerts : true -- Set to false to stop any alerts from appearing
  15. property kasServerAlerts : true -- Set to false to stop warnings about missed servers
  16. property kasFinder : "Finder" -- name of application that can shutdown and restart
  17.  
  18. -- Internal/User setable globals
  19. property gasLinkNeed : true
  20. property kasLinkUserDft : "CasaVision" -- Our friend on all machines
  21. property kasLinkPassDft : "" -- Our friend's password on all machines
  22.  
  23. global gasLinkSet -- Has the friend been looked up?
  24. global gasLinkUser -- Used to connect (KeyChain Lookup)
  25. global gasLinkPass
  26.  
  27.  
  28. -- Internal globals
  29. property gasOurZone : "" -- Set to zone of gasOurAlias
  30. property gasOurServer : "" -- Set to server of gasOurAlias
  31. property gasOurName : "" -- Name of application on remote server
  32. property gasOurAction : "" -- Set to choice of user (quit, shutdown, restart)
  33.  
  34.  
  35. on run
  36.     set gasLinkSet to false
  37.     
  38.     if (gasOurAction is "") then
  39.         ShowInitAlert()
  40.     else if (gasOurName is not "") then
  41.         RemoteAction(gasOurName, gasOurServer, gasOurZone, gasOurAction, {})
  42.     else
  43.         PutAwayVol(gasOurVol)
  44.     end if
  45. end run
  46.  
  47.  
  48. on open fsObjs
  49.     set gasLinkSet to false
  50.     
  51.     if (gasOurName is "") or (option key down of (input state)) then
  52.         if the number of items in fsObjs is not 1 then
  53.             ShowInitAlert()
  54.         else if (gasOurName is "") or ShowChoice("You have the option key pressed." & return & return & ¬
  55.             "Do you wish to reinitialize this script with the object you dropped on it?") then
  56.             SetScriptAlias(item 1 of fsObjs)
  57.         end if
  58.     else
  59.         RemoteAction(gasOurName, gasOurServer, gasOurZone, gasOurAction, fsObjs)
  60.     end if
  61. end open
  62.  
  63.  
  64. on RemoteAction(appName, serverName, zoneName, whatToDo, objsToOpen)
  65.     -- Get the user/password
  66.     GetFriend(false)
  67.     
  68.     if (serverName is not "") then ¬
  69.         talk as user gasLinkUser ¬
  70.             with password gasLinkPass ¬
  71.             on server serverName ¬
  72.             in AppleTalk zone zoneName
  73.     
  74.     if serverName is "" then
  75.         set suspect to application appName
  76.     else
  77.         set suspect to application appName ¬
  78.             of machine serverName ¬
  79.             of zone zoneName
  80.     end if
  81.     
  82.     try
  83.         -- Don't wait for a response, assume the aliases are correct?
  84.         if (objsToOpen is not {}) then
  85.             repeat with fsObj in objsToOpen
  86.                 tell suspect to open (fsObj as alias)
  87.             end repeat
  88.         else if (whatToDo is "Shutdown") then
  89.             tell suspect to «event fndrshut»
  90.         else if (whatToDo is "Restart") then
  91.             tell suspect to «event fndrrest»
  92.         else if (whatToDo is "Quit") then
  93.             tell suspect to quit
  94.         else
  95.             beep 2
  96.         end if
  97.     on error err
  98.         ShowConnectAlert(appName, err)
  99.     end try
  100. end RemoteAction
  101.  
  102.  
  103. on ChooseScriptAlias()
  104.     if ShowChoices("Do you wish to activate this script with a folder/volume or a file?", {"Folder", "File"}) is "File" then
  105.         set myObj to ¬
  106.             choose file with prompt "Choose a server file for this PowerScript RA"
  107.     else
  108.         set myObj to ¬
  109.             choose folder with prompt "Choose a server folder or volume for this PowerScript RA"
  110.     end if
  111.     
  112.     SetScriptAlias(myObj)
  113. end ChooseScriptAlias
  114.  
  115.  
  116. on SetScriptAlias(aliasObj)
  117.     -- My Path
  118.     set myPath to (path to me)
  119.     
  120.     -- Grab an alias
  121.     set aInfo to alias info from aliasObj
  122.     set fInfo to basic info for aliasObj
  123.     
  124.     -- Save the info we need
  125.     set gasOurServer to alias server of aInfo
  126.     set gasOurZone to alias zone of aInfo
  127.     set gasOurVol to alias volume of aInfo
  128.     set gasOurName to original name of aInfo
  129.     
  130.     -- Set our name to dropped name
  131.     set gasLinkNeed to true
  132.     set newName to (catalog name of fInfo)
  133.     
  134.     if (catalog kind of fInfo) is a folder then
  135.         set gasOurAction to "Put Away"
  136.         set gasLinkNeed to false
  137.         set newName to gasOurVol
  138.     else if (gasOurName is kasFinder) then
  139.         set choice to display dialog "What do you wish to have the " & kasFinder & ¬
  140.             " on " & gasOurServer & ¬
  141.             " do when this script is run?" buttons ¬
  142.             {"Quit", "Restart", "Shutdown"} default button 3
  143.         set gasOurAction to button returned of choice
  144.         if (gasOurAction is not "Quit") then set newName to gasOurServer
  145.     else
  146.         set gasOurAction to "Quit"
  147.     end if
  148.     
  149.     -- Get the user/password
  150.     GetFriend(false)
  151.     
  152.     -- Set our icon to dropped icon
  153.     set ourIcon to (the icon for aliasObj)
  154.     set the icon of myPath to ourIcon with compatibility
  155.     
  156.     -- Refresh our new status in the finder
  157.     try
  158.         tell application "Finder" to update myPath -- only works with OS 8
  159.     on error
  160.         beep
  161.     end try
  162.     
  163.     -- Set our new name
  164.     set newName to "≈ " & gasOurAction & " " & newName
  165.     if (length of newName > 31) then set newName to the text from character 1 to 31 of newName
  166.     
  167.     try
  168.         collate myPath renaming it to newName
  169.     on error
  170.         beep
  171.     end try
  172. end SetScriptAlias
  173.  
  174.  
  175. on GetFriend(override) -- Should later use some modifier key to override?!?
  176.     if (gasOurServer is "") then -- Local?
  177.         set gasLinkSet to true
  178.         set gasLinkUser to kasLinkUserDft
  179.         set gasLinkPass to kasLinkPassDft
  180.     else if (gasLinkNeed) then
  181.         GetOneFriend(override)
  182.     end if
  183. end GetFriend
  184.  
  185.  
  186. on GetOneFriend(override)
  187.     set isLink to true -- Only linking in this script!
  188.     set userMode to "linking"
  189.     set passButtons to {"Cancel", "OK"}
  190.     set passButton to 2
  191.     
  192.     if (gasLinkSet) then
  193.         set defUser to gasLinkUser
  194.         set defPass to gasLinkPass
  195.     else
  196.         set defUser to kasLinkUserDft
  197.         set defPass to kasLinkPassDft
  198.     end if
  199.     
  200.     set usrPwd to KeyChainLookUp(gasOurZone, gasOurServer, isLink)
  201.     
  202.     if (override or usrPwd is {}) then
  203.         set chosen to display dialog ¬
  204.             "Enter the friendly " & userMode & ¬
  205.             " user's name…" default answer defUser ¬
  206.             default button 2 with icon note
  207.         
  208.         if (the button returned of chosen is "OK") then
  209.             set defUser to the text returned of chosen
  210.         else
  211.             return
  212.         end if
  213.         
  214.         set chosen to display dialog ¬
  215.             "Enter the friendly " & userMode & ¬
  216.             " user's password…" buttons passButtons ¬
  217.             default answer defPass default button passButton with icon note
  218.         
  219.         if (the button returned of chosen is not "Cancel") then
  220.             set defPass to the text returned of chosen
  221.         else
  222.             return
  223.         end if
  224.         
  225.         -- Save encrypted user/pass for future access
  226.         KeyChainSave(gasOurZone, gasOurServer, isLink, defUser, defPass, "")
  227.     else
  228.         set defUser to item 1 of usrPwd
  229.         set defPass to item 2 of usrPwd
  230.     end if
  231.     
  232.     set gasLinkSet to true
  233.     set gasLinkUser to defUser
  234.     set gasLinkPass to defPass
  235. end GetOneFriend
  236.  
  237.  
  238. on PutAwayVol(volName)
  239.     -- Volume
  240.     try
  241.         tell application "Finder"
  242.             update item named volName
  243.             pause for 2 with seconds timing -- Give some time to the Finder?
  244.             put away item named volName
  245.         end tell
  246.     on error
  247.         beep
  248.     end try
  249. end PutAwayVol
  250.  
  251.  
  252. on ShowConnectAlert(appName, err)
  253.     if (kasServerAlerts) then
  254.         ShowServerAlert("Couldn't connect to " & appName & " on " & ¬
  255.             gasOurServer & " in zone " & gasOurZone & return & return & ¬
  256.             "The server may be down or the app may not allow remote events." & return & return & "Error was: " & err)
  257.     else
  258.         beep
  259.     end if
  260. end ShowConnectAlert
  261.  
  262.  
  263. on ShowServerAlert(msg)
  264.     set choice to ¬
  265.         display dialog msg buttons {"Reenter Password", "OK"} ¬
  266.             default button 2 with icon stop
  267.     
  268.     if (button returned of choice is not "OK") then GetFriend(true)
  269. end ShowServerAlert
  270.  
  271.  
  272. on ShowInitAlert()
  273.     if ShowChoices("This script must first be activated by dropping exactly one item on it." & ¬
  274.         return & return & "Please see the information file that accompanied it.", ¬
  275.         {"Activate", "OK"}) is not "OK" then ¬
  276.         ChooseScriptAlias()
  277. end ShowInitAlert
  278.  
  279.  
  280. on ShowAlert(msgStr)
  281.     if (kasAllowAlerts) then ¬
  282.         display dialog msgStr buttons {"Damn!"} ¬
  283.             default button 1 with icon stop
  284. end ShowAlert
  285.  
  286.  
  287. on ShowChoice(msgStr)
  288.     return ShowChoices(msgStr, {"No", "Yes"}) is "Yes"
  289. end ShowChoice
  290.  
  291.  
  292. on ShowChoices(msgStr, choices)
  293.     set choice to ¬
  294.         display dialog msgStr buttons choices ¬
  295.             default button (number of items of choices) ¬
  296.             with icon stop
  297.     return (button returned of choice)
  298. end ShowChoices
  299.  
  300.  
  301. property kasKeyChainPassword : "PowerScript" -- Encrypt stored data with this
  302. property kasPrefsFileName : "PowerScript Prefs" -- File name in <Preferences>
  303.  
  304. on KeyChainLookUp(zoneName, serverName, isLinking)
  305.     set prefType to "πSRV"
  306.     if (isLinking) then set prefType to "πLNK"
  307.     
  308.     try
  309.         set myKeyData to load preference of type prefType ¬
  310.             named (zoneName & ":" & serverName) ¬
  311.             in file named kasPrefsFileName
  312.     on error
  313.         return {}
  314.     end try
  315.     
  316.     return (encrypt the data myKeyData ¬
  317.         with password kasKeyChainPassword)
  318. end KeyChainLookUp
  319.  
  320.  
  321. on KeyChainSave(zoneName, serverName, isLinking, usr, pwd, ntPwd)
  322.     set myKey to encrypt the data {usr, pwd, ntPwd} ¬
  323.         with password kasKeyChainPassword
  324.     
  325.     set prefType to "πSRV"
  326.     if (isLinking) then set prefType to "πLNK"
  327.     
  328.     save preference myKey of type prefType ¬
  329.         named (zoneName & ":" & serverName) ¬
  330.         in file named kasPrefsFileName
  331. end KeyChainSave
  332.